import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtTeamInfoApi } from '#/api/cmt/team/info';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getRangePickerDefaultProps } from '#/utils';
import { getBaseCommunityOptions } from '#/api/cmt/base/community';
import { getMerchantInfoOptions } from '#/api/cmt/base/merchantInfo';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'communityId',
      label: '社区ID',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择社区',
        api: getBaseCommunityOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户ID',
      component: 'ApiSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请选择商户',
        api: getMerchantInfoOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'name',
      label: '团队名称',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入团队名称',
      },
    },
    {
      fieldName: 'logo',
      label: '团队logo',
      component: 'ImageUpload',
      formItemClass: 'col-span-1',
    },
    {
      fieldName: 'teamType',
      label: '团队类型',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        options: getDictOptions(DICT_TYPE.CMT_TEAM_TYPE, 'number'),
        placeholder: '请选择团队类型',
      },
    },
    {
      fieldName: 'establishDate',
      label: '成立日期',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'datetime',
        placeholder: '请选择成立日期',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        class: '!w-full',
      },
    },
    {
      fieldName: 'teamSize',
      label: '团队规模',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入团队规模',
      },
    },
    {
      fieldName: 'memberCount',
      label: '成员数量',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入成员数量',
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'introduction',
      label: '团队简介',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 3,
        placeholder: '请输入团队简介',
      },
    },
    {
      fieldName: 'featuredService',
      label: '特色服务',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 2,
        placeholder: '请输入特色服务',
      },
    },
    {
      fieldName: 'serviceFields',
      label: '服务领域',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入服务领域(JSON数组)',
      },
    },
    {
      fieldName: 'serviceRegion',
      label: '服务区域',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入服务区域',
      },
    },
    {
      fieldName: 'leaderUserId',
      label: '负责人会员ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入负责人会员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'leaderPersonId',
      label: '负责人人员ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入负责人人员ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'leaderName',
      label: '负责人姓名',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入负责人姓名',
      },
    },
    {
      fieldName: 'leaderPosition',
      label: '负责人职位',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入负责人职位',
      },
    },
    {
      fieldName: 'leaderPhone',
      label: '负责人电话',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入负责人电话',
      },
    },
    {
      fieldName: 'leaderEmail',
      label: '负责人邮箱',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入负责人邮箱',
      },
    },
    {
      fieldName: 'address',
      label: '地址',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入地址',
      },
    },
    {
      fieldName: 'totalActivities',
      label: '累计活动数',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入累计活动数',
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'totalServiceHours',
      label: '累计服务时长',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入累计服务时长',
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'rating',
      label: '口碑评分',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入口碑评分',
        controlsPosition: 'right',
        class: '!w-full',
        min: 0,
        max: 5,
        step: 0.1,
      },
      defaultValue: 0,
    },
    {
      fieldName: 'recruitmentStatus',
      label: '招募状态',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: getDictOptions(DICT_TYPE.CMT_RECRUITMENT_STATUS, 'number'),
      },
      defaultValue: 0,
    },
    {
      fieldName: 'auditStatus',
      label: '审核状态',
      component: 'Select',
      formItemClass: 'col-span-1',
      componentProps: {
        options: getDictOptions(DICT_TYPE.CMT_AUDIT_STATUS, 'number'),
        placeholder: '请选择审核状态',
      },
    },
    {
      fieldName: 'auditUserId',
      label: '审核人ID',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入审核人ID',
        controlsPosition: 'right',
        class: '!w-full',
      },
    },
    {
      fieldName: 'auditTime',
      label: '审核时间',
      component: 'DatePicker',
      formItemClass: 'col-span-1',
      componentProps: {
        type: 'datetime',
        placeholder: '请选择审核时间',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        class: '!w-full',
      },
    },
    {
      fieldName: 'auditRemark',
      label: '审核备注',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 2,
        placeholder: '请输入审核备注',
      },
    },
    {
      fieldName: 'teamStatus',
      label: '团队状态',
      component: 'RadioGroup',
      formItemClass: 'col-span-1',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
      },
      defaultValue: 1,
    },
    {
      fieldName: 'sort',
      label: '排序',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入排序',
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'communityId',
      label: '社区ID',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择社区',
        api: getBaseCommunityOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'merchantId',
      label: '商户ID',
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择商户',
        api: getMerchantInfoOptions,
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
      },
    },
    {
      fieldName: 'name',
      label: '团队名称',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入团队名称',
      },
    },
    {
      fieldName: 'teamType',
      label: '团队类型',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: getDictOptions(DICT_TYPE.CMT_TEAM_TYPE, 'number'),
        placeholder: '请选择团队类型',
      },
    },
    {
      fieldName: 'leaderName',
      label: '负责人姓名',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入负责人姓名',
      },
    },
    {
      fieldName: 'leaderPhone',
      label: '负责人电话',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入负责人电话',
      },
    },
    {
      fieldName: 'recruitmentStatus',
      label: '招募状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: getDictOptions(DICT_TYPE.CMT_RECRUITMENT_STATUS, 'number'),
        placeholder: '请选择招募状态',
      },
    },
    {
      fieldName: 'auditStatus',
      label: '审核状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: getDictOptions(DICT_TYPE.CMT_AUDIT_STATUS, 'number'),
        placeholder: '请选择审核状态',
      },
    },
    {
      fieldName: 'teamStatus',
      label: '团队状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择团队状态',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtTeamInfoApi.TeamInfo>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '团队ID',
      minWidth: 120,
    },
    {
      field: 'communityId',
      title: '社区ID',
      minWidth: 120,
    },
    {
      field: 'merchantId',
      title: '商户ID',
      minWidth: 120,
    },
    {
      field: 'name',
      title: '团队名称',
      minWidth: 150,
    },
    {
      field: 'logo',
      title: '团队logo',
      minWidth: 100,
      cellRender: {
        name: 'CellImage',
        props: { size: 60 },
      },
    },
    {
      field: 'teamType',
      title: '团队类型',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.CMT_TEAM_TYPE },
      },
    },
    {
      field: 'establishDate',
      title: '成立日期',
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      field: 'teamSize',
      title: '团队规模',
      minWidth: 120,
    },
    {
      field: 'memberCount',
      title: '成员数量',
      minWidth: 100,
    },
    {
      field: 'introduction',
      title: '团队简介',
      minWidth: 200,
    },
    {
      field: 'leaderName',
      title: '负责人姓名',
      minWidth: 120,
    },
    {
      field: 'leaderPhone',
      title: '负责人电话',
      minWidth: 130,
    },
    {
      field: 'totalActivities',
      title: '累计活动数',
      minWidth: 120,
    },
    {
      field: 'totalServiceHours',
      title: '累计服务时长',
      minWidth: 140,
    },
    {
      field: 'rating',
      title: '口碑评分',
      minWidth: 100,
    },
    {
      field: 'recruitmentStatus',
      title: '招募状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.CMT_RECRUITMENT_STATUS },
      },
    },
    {
      field: 'auditStatus',
      title: '审核状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.CMT_AUDIT_STATUS },
      },
    },
    {
      field: 'teamStatus',
      title: '团队状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'sort',
      title: '排序',
      minWidth: 80,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
